<%@page import="java.util.LinkedList"%>
<%@page import="com.charlie.beans.Roles"%>
<%@page import="java.util.List"%>

<%!
    String checkRole(List<String> roles, Roles role) {
        return roles.contains(role.getRolesName()) ? "checked=\"checked\"" : "";
    }

%>

<%@include file="/header.jsp"%>

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.11&libraries=places&sensor=false"></script>
<script type="text/javascript" src="/resources/js/signup.js"></script>
<% if (request.getAttribute("validate") != null) {%>
<script type="text/javascript">
    $(function() {
        email_exist = "${email_exist}";
        $("form").validationEngine('validate');
    });
</script>
<% }%>
<%
    String id = request.getParameter("userId");
    Users u = (Users) request.getAttribute("current_user");
%>

<div class="left_side">
    <jsp:include page="/blocks/conditionalLeftBar.jsp" />
</div>

<div class="right_side">
    <h2 class="content_title" style="font-size: x-large;">Edit user</h2>

    <div class="white_block">
        <h2 style="font-size: large; text-align: center;">Information about User</h2>
        <form method="post">
            First name<br>
            <input type="text" name="first_name" size="95%" class="validate[required,minSize[1],maxSize[32],funcCall[check_name]]" value="<%=u.getFirstName()%>"><br>
            Last name<br>
            <input type="text" name="second_name" size="95%" class="validate[required,minSize[1],maxSize[32],funcCall[check_name]]" value="<%=u.getSecondName()%>"><br>
            Location<br>
            <input type="text" name="location" id="location" size="95%" class="validate[required]" value="<%=u.getLocation()%>"><br>
            E-mail<br>
            <input type="email" name="email" size="95%" class="validate[required,custom[email],funcCall[check_email_exist]]" value="<%=u.getEmail()%>"><br>
            Password<br>
            <input type="password" name="pass" size="95%" id="pass" class="validate[required,minSize[1],maxSize[32]]" value="<%=u.getPassword()%>"><br>
            Password again<br>
            <input type="password" name="pass2" size="95%" class="validate[required,minSize[1],maxSize[32],equals[pass]]" value="<%=u.getPassword()%>">
            <br>
            <br>

            <%
                List<Roles> roles = (List<Roles>) request.getAttribute("rolesList");
                List<String> userRolesName = (List<String>) request.getAttribute("userRolesName");
            %>
            <table class="typycal_table">

                <span class="checkRole"> 
                    Own user roles
                    <%
                        if (roles != null)
                            for (Roles r : roles) {
                                if (!r.getRolesName().equals("Admin")) {
                    %>
                    <td>
                        <input type="checkbox" name="id" <%= checkRole(userRolesName, r)%> value="<%=r.getRolesName()%>"><%=r.getRolesName()%></label>
                    </td> 

                    <%
                            }
                        }
                    else {
                    %>
                    <br> no roles  in system
                    <%      }%>
                </span>

                <td><input style="float: right;" type="submit" size="40" value="Submit"></td>
            </table>
        </form>
    </div>

</div>

<%@include file="/footer.jsp" %>
